package com.situ.studentmanagement.controller;

import com.mysql.fabric.xmlrpc.base.Data;
import com.octo.captcha.Captcha;
import com.octo.captcha.engine.CaptchaEngine;
import com.situ.studentmanagement.CapchaHelper;
import com.situ.studentmanagement.dao.GoodsMapper;
import com.situ.studentmanagement.dao.OrderMapper;
import com.situ.studentmanagement.model.Goods;
import com.situ.studentmanagement.model.Orderone;
import com.situ.studentmanagement.model.Ordertwo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.annotation.Order;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.imageio.ImageIO;
import javax.servlet.http.HttpSession;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static com.situ.studentmanagement.Constants.*;

/**
 * Created by baba on 2016/8/2.
 */
@RestController
@RequestMapping("/login") //这个controller对应到路径 /
public class login {
    @Autowired
    private OrderMapper orderMapper;
    @Autowired
    private GoodsMapper goodsMapper;

    @RequestMapping("/captcha_img")
    public @ResponseBody
    byte[] getCaptchaImgData(HttpSession session) throws IOException {
        //产生验证码图片

        //获取验证码背景图片的路径，这路径放了很多作为背景的图像
        String captcha_backgrounds = session.getServletContext().getRealPath("/WEB-INF/img/captcha");
        CaptchaEngine ce = CapchaHelper.getCaptchaEngine(captcha_backgrounds);
        //需要admin网页中用js定时从服务端获取当前的验证码
        Captcha captcha = ce.getNextCaptcha();
        //为了验证，把captcha对象放到session中，以在客户端提交验证码时进行验证
        session.setAttribute("captcha", captcha);
        //获取验证码图片，这是未压缩的位图
        BufferedImage image = (BufferedImage) captcha.getChallenge();
        ByteArrayOutputStream bao=new ByteArrayOutputStream();
        //应缩成jpg并写到输出流中
        ImageIO.write(image, "jpg", bao);
        return bao.toByteArray();
    }
    @RequestMapping("/addtocar")
    public Map addtocar(Model model, HttpSession session,
                           @RequestParam("id")int goods_id){
        Map map = new HashMap();
        int message=1;
        if(session.getAttribute(USER_NAME)==null){
            message=0;
            map.put("message",message);
        }else {
            //将传过来的商品ID加到购物车中并客户端显示已加入购物车
            int price=0;
            Integer goodid=goods_id;
            Goods goods=goodsMapper.selectPriceById(goodid);
            //判断商品是不是特价商品并取得商品的价格
            if(goods.getIsfree()==1){
                price=goods.getFreeprice();
            }else {
                price=goods.getPrice();
            }
            Integer userid= (Integer) session.getAttribute(USER_ID);
            Long ordernumber= (Long) session.getAttribute(ORDER_NUMBER);
            Ordertwo ordertwo=new Ordertwo(0,ordernumber,goods_id,goods.getName(),goods.getImage(),userid,0,price);
            //向数据库中插入详情表
            orderMapper.insertOneOrder(ordertwo);
            session.setAttribute(ORDER_ID, ordertwo.getId());
        }
        return map;
    }
    @RequestMapping("/deletone")
    public Map deletone(Model model, HttpSession session,
                           @RequestParam("id")int goods_id){
        Map map=new HashMap();
        orderMapper.deletone(goods_id);
        map.put("id",goods_id);
        map.put("isdelet",1);
        return map;
    }

    @RequestMapping("/selectAllOrdertwo")
    public Map selectAllOrdertwo(Model model, HttpSession session){
        Map map=new HashMap();
        List<Ordertwo> ordertwos=orderMapper.selectAllOrdertwo();
        map.put("ordertwos",ordertwos);
        return map;
    }

    //删除订单
    @RequestMapping("/deletorderone")
    public Map deletorderone(Model model, HttpSession session,
                                @RequestParam("id")long  goods_id){
        Map map=new HashMap();
        Long ordernumber= (Long) session.getAttribute(ORDER_NUMBER);
        orderMapper.deletOneByOrderId(goods_id);
        orderMapper.deletTwoByOrderId(goods_id);
        map.put("isdelet",1);
        map.put("id",goods_id);
        return map;
    }

    @RequestMapping("/getOtherOrder")
    public Map getOtherOrder(Model model, HttpSession session){
        Map map=new HashMap();
        Date date=new Date(System.currentTimeMillis()-1000);//获取3秒之前到目前数据库中的信息
        DateFormat format=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String time=format.format(date);
        Orderone orderones=orderMapper.selectAllOrderOneByTime(time);
        if(orderones==null){
            map.put("isadd",0);
        }else {
            map.put("orderones",orderones);
        }
        return map;
    }

}
